package com.wang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.entity.TbBbsArticle;
import com.wang.entity.TbBbsLive;
import com.wang.mapper.TbBbsArticleMapper;
import com.wang.mapper.TbBbsLiveMapper;
import com.wang.response.ResultResponse;
import com.wang.service.TbBbsLiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class TbBbsLiveServiceImpl implements TbBbsLiveService {
    @Autowired
    private TbBbsLiveMapper tbBbsLiveMapper;
    @Autowired
    private TbBbsArticleMapper tbbbsArticleMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResultResponse insertbbslive(TbBbsLive tbBbsLive) {
        //对参数进行非空验证
        if (tbBbsLive.getUserId() == null && tbBbsLive.getUserId() <= 0) {
            throw new RuntimeException("没有登录");
        }
        //查询用户是否点赞过该文章
        QueryWrapper<TbBbsLive> tbBbsLiveQueryWrapper = new QueryWrapper<>();
        tbBbsLiveQueryWrapper.eq("file_id",tbBbsLive.getFileId());
        tbBbsLiveQueryWrapper.eq("user_id", tbBbsLive.getUserId());
//        TFabulous tFabulous = tFabulousMapper.selectOneByExample(example);
        TbBbsLive tbBbsLive1 = tbBbsLiveMapper.selectOne(tbBbsLiveQueryWrapper);
        //查询该文章获取点赞数量
//        TbBbsArticle tbBbsArticle = new TbBbsArticle();
        QueryWrapper<TbBbsArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("file_id", tbBbsLive.getFileId());
        TbBbsArticle  tbBbsArticle= tbbbsArticleMapper.selectOne(wrapper);


        if (null == tbBbsArticle) {
            throw new RuntimeException("该文章不存在");
        }
        //线程锁
//        Lock lock = lockService.getLock(reportId.toString());
//            lock.acquire();
            //如果用户没有点赞过，则增加用户点赞记录以及增加文章表中的点赞数量
            if (null == tbBbsLive1) {
                int insert = tbBbsLiveMapper.insert(tbBbsLive);
                if (insert > 0) {
                    tbBbsArticle.setBbsLive(tbBbsArticle.getBbsLive() + 1);
                    tbbbsArticleMapper.updateById(tbBbsArticle);
                }
                //如果用户点赞过，则删除用户点赞记录以及文章表中的点赞数量减一
            } else {
                int delete = tbBbsLiveMapper.deleteById(tbBbsLive1.getLiveId());
                if (delete > 0) {
                    tbBbsArticle.setBbsLive(tbBbsArticle.getBbsLive() - 1);
                    tbbbsArticleMapper.updateById(tbBbsArticle);
                }
            }

        //再次查询点赞数量
        QueryWrapper<TbBbsArticle> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("file_id", tbBbsLive.getFileId());
        TbBbsArticle  tbBbsArticle1= tbbbsArticleMapper.selectOne(wrapper1);
//        return new FabulousVo().setFabulousNumber(getReportNumber.getLikeNumber());
         return ResultResponse.SUCCESS(tbBbsArticle1.getBbsLive());

    }


    @Override
    public ResultResponse findbbslive(TbBbsLive tbBbsLive) {
        QueryWrapper<TbBbsLive> wrapper = new QueryWrapper<>();
        ArrayList list = new ArrayList();
        wrapper.eq("user_id", tbBbsLive.getUserId());
        List<TbBbsLive> bbsLives = tbBbsLiveMapper.selectList(wrapper);
        for (TbBbsLive live : bbsLives) {
            Integer fid = live.getFileId();
            QueryWrapper<TbBbsArticle> articleQueryWrapper = new QueryWrapper<>();
            articleQueryWrapper.eq("file_id", fid);
            List<TbBbsArticle> bbsArticles = tbbbsArticleMapper.selectList(articleQueryWrapper);
            list.add(bbsArticles);
        }
        return ResultResponse.SUCCESS(list);
    }

    @Override
    public ResultResponse findbbsliveall(TbBbsLive tbBbsLive) {
        Page<TbBbsLive> tbBbsLivePage = new Page<>(tbBbsLive.getPagenum(), tbBbsLive.getPagesize());
        Page<TbBbsLive> page = tbBbsLiveMapper.selectPage(tbBbsLivePage, null);
        long total = page.getTotal();
        return ResultResponse.SUCCESS(page.getRecords(),total);
    }
}
